package com.dongdongsongcan.app.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dongdongsongcan.app.R;
import com.dongdongsongcan.app.common.DDConstant;
import com.dongdongsongcan.app.control.OrderControl;
import com.dongdongsongcan.app.control.bean.OrderDetailData;
import com.dongdongsongcan.app.control.bean.OrderListData;
import com.dongdongsongcan.app.fragment.UserFragment;
import com.dongdongsongcan.app.view.OrderDetailFoodItem;
import com.su.dolphin.control.task.CtrlTask;
import com.su.dolphin.utils.mgr.DpActivityManager;
import com.su.dolphin.utils.mgr.DpFragmentManager;
import com.su.dolphin.utils.ui.TipsUtil;
import com.su.dolphin.view.activity.BaseActivity;

import java.util.List;

public class OrderDetailActivtiy extends BaseActivity {
    private String mOrderId;
    private TextView orderdetail_reveiver;
    private TextView orderdetail_orderid;
    private TextView orderdetail_reveiver_phone;
    private TextView orderdetail_address;
    private TextView orderdetail_payway;
    private TextView orderdetail_ordertime;
    private TextView orderdetail_ordernote;
    private TextView orderdetail_shop;
    private LinearLayout orderdetail_foods;
    private TextView orderdetail_deliver_fee;
    private TextView orderdetail_pkg_discount;
    private TextView orderdetail_total_price;
    private Button orderdetail_status;

    private OrderDetailData orderDetailData;
    private TextView orderdetail_pkg_fee;

    public static void startFrom(Context context, String orderId) {
        Bundle bundle = new Bundle();
        bundle.putString("orderId", orderId);
        DpActivityManager.i().startActivity(context, OrderDetailActivtiy.class, bundle);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        mOrderId = this.getIntent().getExtras().getString("orderId");
        setContentView(R.layout.activity_order_detail);
        super.onCreate(savedInstanceState);
    }


    @Override
    public void initView() {
        orderdetail_status = (Button) findViewById(R.id.orderdetail_status);
        orderdetail_reveiver = (TextView) findViewById(R.id.orderdetail_reveiver);
        orderdetail_orderid = (TextView) findViewById(R.id.orderdetail_orderid);
        orderdetail_reveiver_phone = (TextView) findViewById(R.id.orderdetail_reveiver_phone);
        orderdetail_address = (TextView) findViewById(R.id.orderdetail_address);
        orderdetail_payway = (TextView) findViewById(R.id.orderdetail_payway);
        orderdetail_ordertime = (TextView) findViewById(R.id.orderdetail_ordertime);
        orderdetail_ordernote = (TextView) findViewById(R.id.orderdetail_ordernote);
        orderdetail_shop = (TextView) findViewById(R.id.orderdetail_shop);
        orderdetail_foods = (LinearLayout) findViewById(R.id.orderdetail_foods);
        orderdetail_deliver_fee = (TextView) findViewById(R.id.orderdetail_deliver_fee);
        orderdetail_pkg_discount = (TextView) findViewById(R.id.orderdetail_pkg_discount);
        orderdetail_total_price = (TextView) findViewById(R.id.orderdetail_total_price);
        orderdetail_pkg_fee = (TextView) findViewById(R.id.orderdetail_pkg_fee);


    }

    @Override
    public void setListener() {
        orderdetail_status.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OrderStatusActivtiy.startFrom(OrderDetailActivtiy.this, orderDetailData.getResult().getOrder_number());
            }
        });
    }

    @Override
    public void initData() {
        OrderControl.order_info(mOrderId, new CtrlTask.IControlResponse<OrderDetailData>() {
            @Override
            public void onControlResponse(OrderDetailData data) {
                if (data.isOk()) {
                    OrderDetailData.OrderDetail order = data.getResult();
                    orderDetailData = data;
                    orderdetail_payway.setText(order.getOrder_pay_cash());
                    orderdetail_pkg_discount.setText(order.getYouhui() + "元");
                    orderdetail_reveiver.setText(order.getOrder_name() + order.getOrder_sex());
                    orderdetail_orderid.setText(order.getOrder_number() + "");
                    orderdetail_reveiver_phone.setText(order.getOrder_mobile());
                    orderdetail_address.setText(order.getOrder_area() + order.getOrder_louhao1());
                    orderdetail_ordertime.setText(data.getResult().getOrder_time());
                    orderdetail_shop.setText(data.getResult().getShop_name());
                    orderdetail_ordernote.setText(order.getOrder_beta());
                    orderdetail_total_price.setText(order.getOrder_total_pay());
                    orderdetail_deliver_fee.setText(order.getOrder_addprice());
                    orderdetail_pkg_fee.setText(DDConstant.RMB + order.getOrder_bag());
                    List<OrderDetailData.Food> foods = data.getResult().getIds();
                    for (int i = 0; i < foods.size(); i++) {
                        OrderDetailFoodItem orderDetailFoodItem = new OrderDetailFoodItem(OrderDetailActivtiy.this);
                        orderDetailFoodItem.setInfo(foods.get(i));
                        orderdetail_foods.addView(orderDetailFoodItem.getView());
                    }


                } else {
                    TipsUtil.toast(data.getMsg());
                }

            }
        });

    }

}
